<template>
  <div class="content" v-loading="loading">
    <!-- 上侧 -->
    <div class="top-module-box">
      <div class="left-logo-box">
        <img src="@/assets/image/9999.png" alt />
        <!-- <p>成都市规划和自然资源局办公门户</p> -->
      </div>
    </div>
    <!-- 中间 -->
    <transition name="fades">
      <div class="center-module-box" v-show="flag">
        <div class="center-box">
          <div class="center-module">
            <div class="top-logo-box">
              <!-- <img src="@/assets/image/Login/icon-right-logo.png" alt /> -->
              <div>系统登录</div>
            </div>
            <div class="form-box">
              <!-- <div style="width: 100%; padding-bottom: 20px">
            <el-select
              v-model="loginType"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option label="账户登陆" value="0"></el-option>
              <el-option label="验证码登陆" value="1"></el-option>
            </el-select>
              </div>-->
              <!-- 顶部选项卡 -->
              <div class="module-title">
                <p
                  @click="loginType = 0"
                  :style="{
                color: loginType == 0 ? '#2F6CDF' : '#505050',
                'border-color': loginType == 0 ? '#2F6CDF' : 'transparent',
              }"
                >账户登录</p>
                <p
                  @click="loginType = 1"
                  :style="{
                color: loginType == 1 ? '#2F6CDF' : '#505050',
                'border-color': loginType == 1 ? '#2F6CDF' : 'transparent',
              }"
                >验证码登录</p>
              </div>
              <div style="width: 100%" v-show="loginType == '0'">
                <el-form ref="login" :model="login" :rules="rules">
                  <el-form-item prop="name" style="margin-bottom:3vh">
                    <el-input
                      prefix-icon="el-icon-user"
                      v-model="login.name"
                      placeholder="用户名"
                      @keyup.enter.native="passworldLogin('login')"
                    ></el-input>
                  </el-form-item>
                  <el-form-item prop="passworld">
                    <el-input
                      prefix-icon="el-icon-lock"
                      v-model="login.passworld"
                      placeholder="密码"
                      type="password"
                      @keyup.enter.native="passworldLogin('login')"
                    ></el-input>
                  </el-form-item>
                </el-form>
              </div>
              <div style="width: 100%;" v-show="loginType == '1'">
                <el-form ref="loginAuthCode" :model="loginAuthCode" :rules="rules">
                  <el-form-item prop="phone" style="margin-bottom:3vh">
                    <el-input
                      v-model="loginAuthCode.phone"
                      placeholder="电话号码"
                      @keyup.enter.native="phoneLogin('loginAuthCode')"
                    ></el-input>
                  </el-form-item>
                  <div
                    style="
                  display: flex;
                  align: center;
                  justify-content: space-between;
                "
                  >
                    <div style="width: 45%;padding-right:10px">
                      <el-form-item prop="authCode">
                        <el-input
                          v-model="loginAuthCode.authCode"
                          placeholder="验证码"
                          @keyup.enter.native="phoneLogin('loginAuthCode')"
                        ></el-input>
                      </el-form-item>
                    </div>
                    <div style="width: 45%" @click="sendCode('loginAuthCode')">
                      <authCodeBtn ref="authBtn" @sendCode="sendCodeCallBack"></authCodeBtn>
                    </div>
                  </div>
                </el-form>
              </div>
            </div>
            <div class="bottom-do-box">
              <div>
                <el-checkbox v-model="saveuser" v-show="loginType=='0'?true:false">记住密码</el-checkbox>
              </div>
              <!-- <p>忘记密码</p> -->
            </div>
            <div>
              <div class="login-btn" @click="passworldLogin('login')" v-if="loginType == '0'">
                <p>登录</p>
              </div>
              <div class="login-btn" @click="phoneLogin('loginAuthCode')" v-if="loginType == '1'">
                <p>登录</p>
              </div>
            </div>
          </div>
          <p class="hint-box">
            建议使用 360 浏览器
            <span @click="download">点击下载</span>
          </p>
        </div>
      </div>
    </transition>
    <!-- 底部 -->
    <div class="bottom-module-box">
      <div class="left-bottom-box">
        <!-- <div class="left-bottom-list">
          <p>开放平台</p>
          <p>公共资源</p>
          <p>意见反馈</p>
          <p>帮助中心</p>
        </div>-->
        <div class="left-bottom-hint">
          <p>
            本站由成都市规划和自然资源信息中心维护&nbsp;&nbsp;服务热线：61884124
        &nbsp;地址：成都市蜀绣西路69号
          </p>
          <p>国土空间基础信息平台提供基础支撑</p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import authCodeBtn from "@/components/ui/authCodeBtn.vue";
export default {
  components: { authCodeBtn },
  data() {
    return {
      flag: false,
      tabState: 1,
      rules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        passworld: [{ required: true, message: "请输入密码", trigger: "blur" }],
        phone: [{ required: true, message: "请输入电话号码", trigger: "blur" }],
        authCode: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      },
      login: {
        name: "",
        passworld: ""
      },
      loginAuthCode: {
        phone: "",
        authCode: ""
      },
      loading: false,
      loginType: "0",
      saveuser: false,
      isAutoLoginFlag:true
    };
  },
  created() {
      var userAgent = navigator.userAgent;
      var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1;
      if(isIE) {
          var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
          reIE.test(userAgent);
          var fIEVersion = parseFloat(RegExp["$1"]);
          if(fIEVersion == 7) {
            console.log("ie")
              this.ydtz()
          } else if(fIEVersion == 8) {
            console.log("ie8")
              this.ydtz()
          } else if(fIEVersion == 9) {
            console.log("ie9")
              this.ydtz()
          } else if(fIEVersion == 10) {
            console.log("ie10")
              this.ydtz()
          } else {
            console.log("ie6")
              this.ydtz()//IE版本<=7
          }   
      }
  },
  methods: {
    sendCodeCallBack(){
      this.$message.success("验证码发送成功");
    },
    ydtz(){
      $.alert.alert('您的IE版本过低，请更换浏览器！','温馨提示','确定',function(){
        window.location.href='http://172.18.3.11/mhoffice/360.html'
      })
    },
    //记住我
    rememberMe(){
     
      
      var rememberMe = this.$Cookies.get("chksave",{expires:7,path:"/"});
      if(rememberMe=='true'){
        this.login.name = this.$Cookies.get("loginName",{expires:7,path:"/"});
        this.login.passworld = this.$Cookies.get("password",{expires:7,path:"/"});
        this.saveuser = true
      }
    },
    passworldLogin(formName) {
      var that = this;

      //页面表单验证
      this.$refs[formName].validate(valid => {
        if (valid) {
          //   alert("submit!");
          that.loading = true;

          //验证通过进行登陆请求
          that.$.ajax({
            url: that.$API.ajaxPath,
            type: "post",
            data: {
              interfaceName: "antu.newportal-zjx.provider.user.UserProvider",
              methodName: "userLogin",
              args: [
                that.login.name.toUpperCase(),
                that.$md5(that.login.passworld),
                ""
              ]
            },
            dataType: "json",
            success: res => {
              // console.log(res);
              // if(this.saveuser){
              //   this.$.cookie('loginName',that.login.name)
              //   this.$.cookie('password',this.login.passworld)
              //   this.$.cookie('chksave',this.saveuser)
              // }
              that.loading = false;
              if (res.status == "0") {
                console.log(JSON.parse(res.result));
                let userMsg = JSON.parse(res.result);
                console.log(JSON.parse(res.result))

                let USER_ID = userMsg.EMPLOYEE.EMPLOYEE_ID;

                let TOKEN = userMsg.tokenV2;
                localStorage.setItem("USER_ID", USER_ID);
                this.$Cookies.set("loginName", this.login.name,{expires:7,path:"/"})
                this.$Cookies.set("password", this.login.passworld,{expires:7,path:"/"})
                this.$Cookies.set('REGION_ID',userMsg.ORG.REGION_ID,{expires:7,path:'/'})
                //记住我
                this.$Cookies.set("chksave",this.saveuser,{expires:7,path:"/"})
                localStorage.setItem("TOKEN", TOKEN);
                var timestamp=new Date().getTime();
                that.$router.push("/home?rmap="+timestamp);
              } else {
                this.$message({
                  message: res.message,
                  type: "warning"
                });
              }
            },
            error: xhr => {
              console.log(xhr);
              that.loading = false;
            }
          });
        } else {
          // 表单验证失败
          console.log("error submit!!");
          return false;
        }
      });
    },
    sendCode() {
      let that = this;
      if (this.$refs.authBtn._data.countDown < 180) {
        return;
      }
      if (this.loginAuthCode.phone == "") {
        that.$message.error("请输入手机号码");
        return;
      }
      that.sendCodeBack(true);

    },
    sendCodeBack(val) {
      let that = this;
      //发送验证码
      if (val) {
        that.$.ajax({
          url: that.$API.ajaxPath,
          type: "post",
          data: {
            interfaceName: "antu.newportal-zjx.provider.user.UserProvider",
            methodName: "getLoginCode",
            args: [that.loginAuthCode.phone]
          },
          dataType: "json",
          success: res => {
            // console.log(res);
            if (res.result == "手机号未绑定用户!") {
              this.$message({
                message: "手机号未绑定用户!",
                type: "warning"
              });
            }else{
              if(res.status =="0"){
                // this.$message.success("发送短信成功！");
                //验证通过发送验证码
                this.$refs.authBtn.sendCode();
              }
            }
          },
          error: xhr => {
            // console.log(xhr);
          }
        });
      }
    },
    //验证码登陆
    phoneLogin(formName) {
      var that = this;

      //页面表单验证
      this.$refs[formName].validate(valid => {
        if (valid) {
          //   alert("submit!");
          that.loading = true;

          //验证通过进行登陆请求
          that.$.ajax({
            url: that.$API.ajaxPath,
            type: "post",
            data: {
              interfaceName: "antu.newportal-zjx.provider.user.UserProvider",
              methodName: "telCodeLogin",
              args: [that.loginAuthCode.phone, that.loginAuthCode.authCode]
            },
            dataType: "json",
            success: res => {
              // console.log(res);

              if (res.status == "0") {
                if (res.result == "验证码错误!") {
                  this.$message({
                    message: res.result,
                    type: "warning"
                  });
                } else {
                  let resultData = JSON.parse(res.result);

                  let userMsg = JSON.parse(resultData.result);

                  let USER_ID = userMsg.EMPLOYEE.EMPLOYEE_ID;

                  let TOKEN = userMsg.tokenV2;
                  localStorage.setItem("USER_ID", USER_ID);

                  localStorage.setItem("TOKEN", TOKEN);

                  console.log(userMsg);

                  that.$router.push("/home");
                }
              }

              that.loading = false;
            },
            error: xhr => {
              // console.log(xhr);
              that.loading = false;
            }
          });
        } else {
          // 表单验证失败
          console.log("error submit!!");
          return false;
        }
      });
    },
    download() {
      window.location.href =
        "http://172.18.2.232:8888/group1/M00/0A/79/rBICi2CaSjKAeeWSBNArcBtgRuw328.exe";
    }
  },
  created(){
  },
  computed: {},
  watch: {},
  beforeCreate() {},
  beforeMount() {},
  mounted() {
    this.rememberMe();
    setTimeout(() => {
      this.flag = true;
    }, 100);
  },
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {}
};
</script>
<style scoped lang="less">
.content {
  position: relative;
  width: 100%;
  height: 100vh;
  background: url("../assets/image/dlbg.png") no-repeat center;
  background-size: 100% auto;
  display: flex;
  flex-direction: column;
  -webkit-flex-direction: column;
  align-items: center;
  -webkit-align-items: center;
  background-color: #0E1128;

  .top-module-box {
    margin-top: 110px;
  }

  .left-logo-box {
    width: 100%;

    img {
      width: 550px;
      height: 38px;
    }

    p {
      color: rgba(255, 255, 255, 1);
      font-size: 25px;
      font-weight: bold;
    }
  }

  .left-bottom-box {
    width: 100%;
    position: absolute;
    bottom: 0;
    transform: translateY(-50%);
  }

  .left-bottom-list {
    width: 400px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 auto;

    p {
      color: #ffffff;
      cursor: pointer;
    }
  }

  .left-bottom-hint {
    font-size: 12px;
    color: #fff;
    text-align: center;
  }

  // 过度动画
  .fades-enter,
  .fades-leave-to {
    //进入前，离开后
    opacity: 0;
  }
  .fades-enter-to,
  .fades-leave {
    //进入后，离开前
    opacity: 1;
  }
  .fades-enter-active,
  .fades-leave-active {
    transition: all 2s;
    transition-delay: 1s;
  }

  // 右侧
  .center-module-box {
    position: absolute;
    top: 23vh;
    left: 50%;
    transform: translateX(-50%);
    width: 400px;
    height: 60vh;
    background-color: rgba(255, 255, 255, 0.8);
    box-sizing: border-box;
    border-radius: 8px;

    padding: 0 50px;
  }
  .center-box {
    display: flex;
    align-items: center;
    flex-direction: column;
    -webkit-flex-direction: column;
    -webkit-align-items: center;
    justify-content: center;
    -webkit-justify-content: center;
    position: relative;
    height: 100%;
  }
  .center-module {
    width: 100%;
    height: 100%;
  }

  .top-logo-box {
    text-align: center;
    padding: 5vh 0 3vh;

    img {
      width: 50px;
      height: 50px;
    }

    div {
      font-size: 24px;
      font-weight: bold;
    }

    .title {
      font-size: 18px;
      color: #888888;
      font-weight: normal;
      margin-top: 9px;
    }
  }

  .right-module-box /deep/ .el-form-item__content {
    margin-left: 0 !important;
  }

  .bottom-do-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 4vh 0 5vh;
  }

  .login-btn {
    width: 100%;
    height: 40px;
    box-sizing: border-box;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(42, 130, 228, 1);
    color: #ffffff;
  }

  .hint-box {
    font-size: 14px;
    color: rgba(174, 174, 174, 1);
    text-align: center;
    // position: absolute;
    // bottom: 20px;
    span {
      color: #2a82e4;
      margin-left: 10px;
      cursor: pointer;
    }
  }
}

.module-title {
  text-align: center;
  //   font-weight: bold;
  display: flex;
  justify-content: space-between;
  cursor: pointer;
  margin-bottom: 2vh;

  p {
    width: 50%;
    padding: 20px 0;
    font-size: 20px;
    margin:0;
    color: #333333;
    border-bottom: 3px transparent solid;
    padding-bottom: 1%;
    font-weight: bold;
  }
}
.bottom-module-box {
  width: 100%;
}
*{
  font-size: 14px;
}
</style>
